package com.situ.mall.controller.front;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Cart;
import com.situ.mall.entity.Order;
import com.situ.mall.entity.OrderItem;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.Shipping;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICartService;
import com.situ.mall.service.IOrderItemService;
import com.situ.mall.service.IOrderService;
import com.situ.mall.service.IShippingService;

@Controller
@RequestMapping("/order")
public class FrontOrderController {

/* 2020.04.01 订单确认页面 所需 shippingList cartList */
	@Autowired
	private IShippingService shippingService;
	
	@Autowired
	private ICartService cartService;

/* 2020.04.02 订单确认页 提交订单 ☞   order表insert(order)*/
	@Autowired
	private IOrderService orderService;
	
/* 2020年4月19日 order_list.jsp☞order_info 订单详情  by userId & orderNo & shippingId*/
	@Autowired
	private IOrderItemService orderItemService;

// 2020.04.01--1.cart_list结算☞ order_confirm 获取 用户的      所有地址by userId & 所有购物车 选中的商品by userId checked=1
	@RequestMapping("/getOrderConfirmJsp.shtml")
	public String getOrderConfirmJsp(Model model,HttpSession session) { // 取到 1.shippingList(地址信息 by userId) 2.cartList(购物车里 结算的商品 by userId=HttpSession user.getId & checked=1)
		System.out.println("FrontOrderController.getOrderConfirmJsp()");
				
		User user = (User)session.getAttribute("user");
		Integer userId = user.getId(); 
		
		//1.取 地址集合 by userId (属于这个用户的)
		List<Shipping> shippingList = shippingService.selectOrderConfirmShippingInfoByUserId(userId);
		
		//2.取 购物车内的 商品集合  by userId=HttpSession user.getId & checked=1 (属于这个用户的  & 购物车打钩的)
		Integer checked = 1;
		List<Cart> cartList = cartService.selectOrderConfirmCartListByUserIdAndChecked(userId,checked);
		
		model.addAttribute("shippingList", shippingList);
		model.addAttribute("cartList",cartList );
				
		//3. 转发到 订单详情页面
		return "order_confirm";
	}
	
// 2020年4月2日--2.创建订单 (order_confirm.jsp 点结算)  order信息☞order表   、 orderItem信息☞order_item表 
	@RequestMapping("/insertOrder.shtml")
	@ResponseBody
	public Result insertOrder(Order order, HttpSession session) {
		System.out.println("FrontOrderController.insertOrder()");
		
		//0.前台 Form表单 传过来 shippingId、 paymentType-- order(shippingId、 paymentType)
		System.out.println("有 shippingId、 paymentType: " + order);
		
		//1.添加userId进order Session取， -- order(shippingId、 paymentType、userId)
				User user = (User)session.getAttribute("user");
				Integer userId = user.getId();
				order.setUserId(userId);
				System.out.println("有 shippingId、 paymentType、userId: " + order);

		/* service封装:：1. orderNo、 2.orderStatus、3.orderPayment、4.orderItem(类) */
		boolean isSuccess = orderService.insertOrder(order);
		
		
		//返回结果集
		if (isSuccess) {
			return Result.success("订单创建成功");
		} else {
			return Result.error("订单创建失败");
		}
		
	}// insertOrder()
	

	

/* 2020年4月15日 我的订单☞查询 订单集合orderList by userId */
	@RequestMapping(value = "/getOrderListJsp.shtml")
	public String getOrderListJsp(HttpSession httpSession, Model model) {
		System.out.println("FrontOrderController.getOrderListJsp()");
		
		User user = (User) httpSession.getAttribute("user");
		Integer userId = user.getId();
		System.out.println("userId："+userId);
		
		List<Order> orderList = orderService.selectOrderListByUserId(userId);
		model.addAttribute("orderList",orderList);
		
		return "order_list";
	}
	
	/* 2020年4月19日 order_list.jsp☞order_info 订单详情  by userId & orderNo & shippingId*/
	@RequestMapping(value = "/getOrderInfoJsp.shtml")
	public String getOrderInfoJsp( Long orderNo, Integer shippingId, HttpSession session, Model model ) {
		System.out.println("FrontOrderController.getOrderInfoJsp()");
		System.out.println("orderNo："+orderNo);
		System.out.println("shippingId：" + shippingId);
		
		User user = (User) session.getAttribute("user");
		Integer userId = user.getId();
		
		Order orderInfo = orderService.selectOrderInfoByUserIdAndOrderNo(userId,orderNo);
		Shipping shippingInfo = shippingService.selectShippingAdressByShippingId(shippingId); //&orderNo (前台我的遍历order封装好了shippingId)
		List<OrderItem> orderItemInfoList = orderItemService.selectOrderItemInfoListByUserIdAndOrderNo(userId,orderNo);
		
		System.out.println(shippingInfo);
		
		model.addAttribute("orderInfo", orderInfo);
		model.addAttribute("shippingInfo", shippingInfo);
		model.addAttribute("orderItemInfoList", orderItemInfoList);
		
		return "order_info";
	}
		
}
